﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Golf.WebUI.UserControl
{
    public partial class Login1 : System.Web.UI.Page
    {
        public string Host = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Host = ThumNail.GetHttpHost();
            if (!IsPostBack)
                if (Request.Form["action"] != null)
                    switch (Request.Form["action"])
                    {
                        case "login":
                            Cus_Login();
                            break;
                        case "Create":
                            Create_Cus();
                            break;
                        case "Valid_email":
                            Valid_email();
                            break;
                    }
        }
        #region 登录验证
        private void Cus_Login()
        {
            Golf.BLL.Golf_User bll = new Golf.BLL.Golf_User();
            List<Golf.Model.Golf_User> list = bll.GetModelList("Email like '%" + Server.UrlDecode(Request.Form["email"]) + "%'");//怕的是区分大小写
            bool error_emial = true;//email错误
            bool error_pwd = true;//密码错误
            foreach (var item in list)
            {
                if (item.Email.ToLower().Equals(Server.UrlDecode(Request.Form["email"]).ToLower()))//匹配邮箱
                {
                    error_emial = false;
                    //if (item.PassWord.Equals(ThumNail.Lower32(Server.UrlDecode(Request.Form["email"]))))//匹配密码
                    if (item.PassWord.Equals(Server.UrlDecode(Request.Form["pwd"])))//匹配密码
                    {
                        item.LoginTime = DateTime.Now;
                        bll.Update(item);
                        CookiesHelper.SaveCookie("_Customer_id", Server.UrlEncode(item.Id.ToString()), 7, "D");//保存登录会员的id值
                        CookiesHelper.SaveCookie("_Customer_Email", Server.UrlEncode(item.Email.ToString()), 7, "D");//保存登录会员的Email
                        error_pwd = false;
                    }
                    else
                    {
                        error_pwd = true;
                    }
                }
                else
                {
                    error_emial = true;
                }
            }

            if (error_emial)
            {
                Response.Write("1|");// Email错误
            }
            else if (error_pwd)
            {
                Response.Write("2|");// 密码错误
            }
            else
            {
                Response.Write("3|");////登录成功
            }
        }
        #endregion

        #region  Create_Cus   注册会员
        private void Create_Cus()
        {
            string name = Server.UrlDecode(Request.Form["name"]);
            string Email = Server.UrlDecode(Request.Form["email"]);
            string pwd = Server.UrlDecode(Request.Form["pwd"]);
            string pwd_compare = Server.UrlDecode(Request.Form["pwd_compare"]);
            Golf.BLL.Golf_User bll = new Golf.BLL.Golf_User();
            Golf.Model.Golf_User model = new Golf.Model.Golf_User();
            if (CheckEmail(Email))
            {
                if (pwd.Equals(pwd_compare))
                {
                    if (name != "")
                    {
                        int i = name.IndexOf(' ');
                        if (i != -1)
                        {
                            model.FristName = name.Substring(0, i);
                            model.LastName = name.Substring(i, name.Length - i);
                        }
                        else
                        {
                            model.FristName = name;
                            model.LastName = name;
                        }
                        model.PassWord = pwd;
                        model.RegisterTime = DateTime.Now;
                        model.State = 1;
                        model.UserType = 0;
                        model.Email = Email;
                        model.LoginTime = DateTime.Now;
                        if (bll.Add(model) > 0)
                        {
                            Response.Write("                 Registration success!\n\n Please click on the left side of the log, LOG operator|");
                        }
                        else
                        {
                            Response.Write("                 Registration failed! \n\nPlease Jus|");
                        }
                    }

                }
                else
                {
                    Response.Write("Two input passwords do not match!|");
                }
            }
            else
            {
                Response.Write("The mailbox is already registered!|");
            }
        }


        #endregion

        #region  验证邮箱的方法
        /// <summary>
        /// 验证邮箱的方法
        /// </summary>
        /// <param name="Email">条件邮箱</param>
        public bool CheckEmail(string Email)
        {
            bool flag = true;
            Golf.BLL.Golf_User us = new BLL.Golf_User();
            List<Golf.Model.Golf_User> list = us.DataTableToList(us.GetList(" Email like '%" + Email + "%' and  UserType=0").Tables[0]);
            foreach (var item in list)
            {
                if (Request.Form["email"].ToLower() == item.Email.ToLower())
                {
                    flag = false;
                }
            }
            if (list.Count == 0)
            {
                flag = true;
            }
            return flag;
        }
        public void Valid_email()
        {
            string Email = Server.UrlDecode(Request.Form["email"]);
            if (!CheckEmail(Email))
            {
                Response.Write("no|");
            }
            else
            {
                Response.Write("yes|");
            }
        }
        #endregion

    }
}
